//
//  MTMinePblishCollectionViewCell.swift
//  MT
//
//  Created by ooops on 2020/6/2.
//  Copyright © 2020 张强. All rights reserved.
//
import UIKit
import ZFPlayer
import MTCategory

protocol MTMinePblishCollectionViewCellDelegate: NSObjectProtocol {
    /// 点赞
    func likeEvent(cell: MTMinePblishCollectionViewCell, goodButton: GKLikeView, model: MineInfoListModel?, countLabel: UILabel)
}

class MTMinePblishCollectionViewCell: UICollectionViewCell {
    
    weak var delegate: MTMinePblishCollectionViewCellDelegate?
    
    class func reuseIdentity() -> String {
        return "MTMinePblishCollectionViewCellID"
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        coverImageView.image = nil
    }
    
    var playOrPauseBack: MTHomeDouYinCellPlayBack?
    
    var model: MineInfoListModel? {
        didSet {
            guard let model = model else { return }
            coverImageView.mtSetImage(model.imgList?.first?.coverImg ?? model.imgList?.first?.href, nil)
//            if let list = model.imgList, list.count > 1 {`
//                coverImageView.mtSetImage(model.imgList?.first?.href, nil)
//            } else {
//                coverImageView.mtSetImage(model.imgList?.first?.coverImg, nil)
//            }`
            likeCountLabel.text = "\(model.likeCount)"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        imgView.clipsToBounds = true
        imgView.tag = 8888
        return imgView
    }()
    
    lazy var likeCountLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = .white
        return l
    }()
    
    /// 点赞
    lazy var goodButton: GKLikeView = {
        let btn = GKLikeView(size: CGSize(width: 15, height: 15))
        let tap = UITapGestureRecognizer(target: self, action: #selector(MTMinePblishCollectionViewCell.like))
        btn.addGestureRecognizer(tap)
        return btn
    }()
    
    @objc private func playVideo(sender: UIButton) {
        self.playOrPauseBack?(sender)
    }
    
    @objc private func like() {
        self.delegate?.likeEvent(cell: self, goodButton: goodButton, model: model, countLabel: likeCountLabel)
    }
    
    private func setupUI() {
        contentView.addSubview(coverImageView)
        contentView.addSubview(goodButton)
        contentView.addSubview(likeCountLabel)
    }
    
    private func makeConstraint() {
        coverImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        goodButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        likeCountLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(goodButton)
            make.left.equalTo(goodButton.snp.right).offset(5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
}
